package com.kzf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kzf.mapper.StudentMapper;
import com.kzf.pojo.Student;
import com.kzf.pojo.StudentDTO;
import com.kzf.pojo.StudentQueryParam;
import com.kzf.pojo.StudentResult;
import com.kzf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired private StudentMapper studentMapper;

    @Override
    public StudentResult<Student> getStudent(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> list = studentMapper.getStudent(studentQueryParam);
        Page<Student> p =(Page<Student>) list;
        return new StudentResult<Student>(p.getTotal(),p.getResult());
    }

    @Override
    public void deleteByIds(List<Integer> idList){
        studentMapper.deleteByIds(idList);
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.addStudent(student);
    }

    @Override
    public StudentDTO getStudentById(Integer id) {
        return studentMapper.getStudentById(id);
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    @Override
    public void updateScore(Integer id, Integer score) {
        studentMapper.updateScore(id,score);
    }
}
